#!/bin/bash

PORT="/dev/cu.usbmodem1401"
BAUD=115200
PROJECT_DIR="/Users/xugenyin/file/esp32_learn/esp32-s3-devkitc-1_N16R8"

cd "$PROJECT_DIR" || exit 1

# 检查端口占用并杀掉占用进程
OCCUPY_PIDS=$(lsof -t "$PORT")
if [ -n "$OCCUPY_PIDS" ]; then
    echo "Port $PORT is occupied by PID(s): $OCCUPY_PIDS"
    echo "Killing process(es)..."
    kill -9 $OCCUPY_PIDS
    sleep 1
fi

# 启动 PlatformIO Monitor
echo "Opening PlatformIO Monitor on $PORT at $BAUD baud..."
pio device monitor -p "$PORT" -b "$BAUD"